package bookmanagementsystem.dao;

import bookmanagementsystem.model.User;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAO {
    private static final String URL = "jdbc:mysql://localhost:3306/book_management_system";
    private static final String USERNAME = "root";
    static final String PASSWORD = "root";

    // 用户登录验证方法，修改为返回登录成功的用户对象（包含用户的唯一ID等信息），而不仅仅是布尔值
    public User loginUser(String userID, String password) {
        String sql = "SELECT * FROM user_table WHERE user_id =? AND password =?";
        try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setString(1, userID);
            preparedStatement.setString(2, password);

            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                // 从查询结果中获取用户的各项信息并封装成User对象返回
                String retrievedUserID = resultSet.getString("user_id");
                String retrievedPassword = resultSet.getString("password");
                String gender = resultSet.getString("gender");
                String phoneNumber = resultSet.getString("phone_number");
                String email = resultSet.getString("email");
                return new User(retrievedUserID, retrievedPassword, gender, phoneNumber, email);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 注册用户方法，
    public boolean registerUser(User user) {
        try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO user_table (user_id, password, gender, phone_number, email) VALUES (?,?,?,?,?)")) {

            preparedStatement.setString(1, user.getUserID());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getGender());
            preparedStatement.setString(4, user.getPhoneNumber());
            preparedStatement.setString(5, user.getEmail());

            int rowsAffected = preparedStatement.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
           e.printStackTrace();
            return false;
        }
    }

    // 根据用户ID查询用户信息方法，保持不变，可用于在需要获取完整用户信息的其他场景
    public User getUserById(String userID) {
        String sql = "SELECT * FROM user_table WHERE user_id =?";
        try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setString(1, userID);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                String password = resultSet.getString("password");
                String gender = resultSet.getString("gender");
                String phoneNumber = resultSet.getString("phone_number");
                String email = resultSet.getString("email");
                return new User(userID, password, gender, phoneNumber, email);
            } else {
                System.out.println("未找到用户：" + userID);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("查询用户信息失败，原因: " + e.getMessage());
        }
        return null;
    }

    // 修改用户密码方法
    public boolean updateUserPassword(User user) {
        try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement preparedStatement = connection.prepareStatement("UPDATE user_table SET password =? WHERE user_id =?")) {

            preparedStatement.setString(1, user.getPassword());
            preparedStatement.setString(2, user.getUserID());

            int rowsAffected = preparedStatement.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 更新用户信息方法（更新除用户ID外的其他常用信息，如密码、性别、电话号码、邮箱等），保持不变，可按需优化
    public boolean updateUserInfo(User user) {
        try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement preparedStatement = connection.prepareStatement("UPDATE user_table SET gender =?, phone_number =?, email =? WHERE user_id =?")) {


            preparedStatement.setString(1, user.getGender());
            preparedStatement.setString(2, user.getPhoneNumber());
            preparedStatement.setString(3, user.getEmail());
            preparedStatement.setString(4, user.getUserID());

            int rowsAffected = preparedStatement.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 新增的专门用于修改用户ID的方法（需谨慎使用，因为涉及到关联数据等问题，实际应用中可能需要更多逻辑判断），保持不变，可完善相关逻辑
    
}